< Summary

Class:GDX.Reflection
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Reflection.cs
Covered lines:119
Uncovered lines:3
Coverable lines:122
Total lines:300
Line coverage:97.5% (119 of 122)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:12
Method coverage:91.6% (11 of 12)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Reflection()0%110100%
GetDefault(...)0%3.043083.33%
GetType(...)0%33092.86%
GetTypeQualifiedName(...)0%2100%
InvokeStaticMethod(...)0%4.14081.82%
InvokeMethod(...)0%440100%
SetFieldOrPropertyValue(...)0%4.494068.75%
SetFieldValue(...)0%3.053081.82%
SetPropertyValue(...)0%3.053081.82%
TryGetFieldValue[T](...)0%3.513061.54%
TryGetFieldOrPropertyValue(...)0%5.194057.89%
TryGetPropertyValue[T](...)0%3.513061.54%

File(s)

./Packages/com.dotbunny.gdx/GDX/Reflection.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Reflection;
 7
 8namespace GDX
 9{
 10    /// <summary>
 11    ///     A collection of reflection related helper utilities.
 12    /// </summary>
 13    /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks>
 14    public static class Reflection
 15    {
 16        /// <summary>
 17        ///     <see cref="BindingFlags"/> for a private field.
 18        /// </summary>
 19        public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic;
 20        /// <summary>
 21        ///     <see cref="BindingFlags"/> for a private static.
 22        /// </summary>
 23        public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic;
 24        /// <summary>
 25        ///     <see cref="BindingFlags"/> for a public static.
 26        /// </summary>
 27        public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public;
 28
 29        /// <summary>
 30        ///     The assembly qualified name for <see cref="UnityEngine.Object" />
 31        /// </summary>
 232        public static readonly string UnityObjectName = typeof(UnityEngine.Object).AssemblyQualifiedName;
 33
 34        /// <summary>
 35        ///     The assembly qualified name for <see cref="Serializable.SerializableTypes" />
 36        /// </summary>
 237        public static readonly string SerializedTypesName =
 38            typeof(Serializable.SerializableTypes).AssemblyQualifiedName;
 39
 40        /// <summary>
 41        ///     Returns the default value for a given type.
 42        /// </summary>
 43        /// <param name="type">A qualified type.</param>
 44        /// <returns>The default value.</returns>
 45        public static object GetDefault(this Type type)
 246        {
 247            if (type.IsClass || !type.IsValueType)
 148            {
 149                return null;
 50            }
 151            return Activator.CreateInstance(type);
 252        }
 53
 54        /// <summary>
 55        ///     Returns a qualified type..
 56        /// </summary>
 57        /// <param name="type">The full name of a type.</param>
 58        /// <returns>A <see cref="System.Type"/> if found.</returns>
 59        public static Type GetType(string type)
 2960        {
 2961            Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
 2962            int loadedAssembliesCount = loadedAssemblies.Length;
 511663            for (int i = 0; i < loadedAssembliesCount; i++)
 255664            {
 255665                Type targetType = loadedAssemblies[i].GetType(type);
 255666                if (targetType != null)
 2767                {
 2768                    return targetType;
 69                }
 252970            }
 271            return null;
 2972        }
 73
 74        /// <summary>
 75        ///     Returns a short form, non-versioned qualified name for the type.
 76        /// </summary>
 77        /// <param name="type">The <see cref="System.Type"/> to generate the qualified name for.</param>
 78        /// <returns>A qualified name</returns>
 79        public static string GetTypeQualifiedName(Type type)
 080        {
 081            return $"{type.FullName}, {type.Assembly.GetName().Name}";
 082        }
 83
 84        /// <summary>
 85        ///     Invokes a known static method.
 86        /// </summary>
 87        /// <param name="type">The explicit type of the static class.</param>
 88        /// <param name="method">The name of the method to invoke.</param>
 89        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 90        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 91        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 92        public static object InvokeStaticMethod(string type, string method, object[] parameters = null,
 93            BindingFlags flags = PublicStaticFlags)
 694        {
 695            Type targetType = GetType(type);
 696            if (targetType != null)
 597            {
 598                MethodInfo targetMethod = targetType.GetMethod(method, flags);
 599                if (targetMethod != null)
 4100                {
 4101                    return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray);
 102                }
 1103            }
 2104            return null;
 6105        }
 106
 107        /// <summary>
 108        ///     Invoke a known private method on an object.
 109        /// </summary>
 110        /// <param name="targetObject">The ambiguous object to invoke a method on.</param>
 111        /// <param name="method">The name of the method to invoke.</param>
 112        /// <param name="parameters">Any parameters that should be passed to the method?</param>
 113        /// <param name="flags">The <paramref name="method"/>'s access flags.</param>
 114        /// <returns>An <see cref="object"/> of the return value. This can be null.</returns>
 115        public static object InvokeMethod(object targetObject, string method, object[] parameters = null,
 116            BindingFlags flags = PrivateFieldFlags)
 11117        {
 11118            Type targetType = targetObject.GetType();
 11119            MethodInfo targetMethod = targetType.GetMethod(method, flags);
 11120            return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null;
 11121        }
 122
 123        /// <summary>
 124        ///     Set the field or property value of a specific <paramref name="targetObject"/>, which may not be
 125        ///     normally accessible.
 126        /// </summary>
 127        /// <param name="targetObject">The instanced object which will have it's field or property value set.</param>
 128        /// <param name="name">The field or property's name to set.</param>
 129        /// <param name="value">The value to set the field or property to.</param>
 130        /// <param name="fieldFlags">The field's access flags.</param>
 131        /// <param name="propertyFlags">The property's access flags.</param>
 132        /// <returns>true/false if the value was set.</returns>
 133        public static bool SetFieldOrPropertyValue(object targetObject, string name, object value,
 134            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 6135        {
 6136            if (targetObject == null)
 1137                return false;
 138
 5139            Type type = targetObject.GetType();
 5140            FieldInfo f = type.GetField(name, fieldFlags);
 5141            if (f != null)
 2142            {
 2143                f.SetValue(targetObject, value);
 2144                return true;
 145            }
 146
 3147            PropertyInfo p = type.GetProperty(name,propertyFlags);
 3148            if (p != null)
 2149            {
 2150                p.SetValue(targetObject, value);
 2151                return true;
 152            }
 153
 1154            return false;
 6155        }
 156
 157        /// <summary>
 158        ///     Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible.
 159        /// </summary>
 160        /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th
 161        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 162        /// <param name="name">The field's name to set.</param>
 163        /// <param name="value">The value to set the field to.</param>
 164        /// <param name="flags">The field's access flags.</param>
 165        /// <returns>true/false if the value was set.</returns>
 166        public static bool SetFieldValue(object targetObject, Type type, string name, object value,
 167            BindingFlags flags = PrivateFieldFlags)
 6168        {
 6169            if (type != null)
 5170            {
 5171                FieldInfo field = type.GetField(name, flags);
 5172                if (field != null)
 4173                {
 4174                    field.SetValue(targetObject, value);
 4175                    return true;
 176                }
 1177            }
 2178            return false;
 6179        }
 180
 181        /// <summary>
 182        ///     Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib
 183        /// </summary>
 184        /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if
 185        /// <param name="type">The type of the <paramref name="targetObject"/>.</param>
 186        /// <param name="name">The property's name to set.</param>
 187        /// <param name="value">The value to set the property to.</param>
 188        /// <param name="flags">The property's access flags.</param>
 189        /// <returns>true/false if the value was set.</returns>
 190        public static bool SetPropertyValue(object targetObject, Type type, string name, object value,
 191            BindingFlags flags = PrivateFieldFlags)
 4192        {
 4193            if (type != null)
 3194            {
 3195                PropertyInfo property = type.GetProperty(name, flags);
 3196                if (property != null)
 2197                {
 2198                    property.SetValue(targetObject, value);
 2199                    return true;
 200                }
 1201            }
 2202            return false;
 4203        }
 204
 205        /// <summary>
 206        ///     Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a
 207        /// </summary>
 208        /// <remarks></remarks>
 209        /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t
 210        /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param>
 211        /// <param name="name">The field's name to read.</param>
 212        /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be
 213        /// <param name="flags">The field's access flags.</param>
 214        /// <typeparam name="T">The type of data being read from the field.</typeparam>
 215        /// <returns>true/false if the process was successful.</returns>
 216        public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl
 7217        {
 7218            if (type == null)
 1219            {
 1220                returnValue = default;
 1221                return false;
 222            }
 6223            FieldInfo fieldInfo = type.GetField(name, flags);
 6224            if (fieldInfo == null)
 1225            {
 1226                returnValue = default;
 1227                return false;
 228            }
 5229            returnValue = (T)fieldInfo.GetValue(targetObject);
 5230            return true;
 7231        }
 232
 233        /// <summary>
 234        ///     Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not
 235        ///     be normally accessible.
 236        /// </summary>
 237        /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks>
 238        /// <param name="targetObject">The instanced object which will have it's field or property value read.</param>
 239        /// <param name="name">The field or property's name to read.</param>
 240        /// <param name="returnValue">The returned value from the field or property, the default value if the property w
 241        /// <param name="fieldFlags">The field's access flags.</param>
 242        /// <param name="propertyFlags">The property's access flags.</param>
 243        /// <returns>true/false if a value was found.</returns>
 244        public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue,
 245            BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags)
 7246        {
 7247            if (targetObject == null)
 1248            {
 1249                returnValue = null;
 1250                return false;
 251            }
 252
 6253            Type type = targetObject.GetType();
 6254            FieldInfo f = type.GetField(name, fieldFlags);
 6255            if (f != null)
 2256            {
 2257                returnValue = f.GetValue(targetObject);
 2258                return true;
 259            }
 260
 4261            PropertyInfo p = type.GetProperty(name,propertyFlags);
 4262            if (p != null)
 3263            {
 3264                returnValue = p.GetValue(targetObject);
 3265                return true;
 266            }
 267
 1268            returnValue = default;
 1269            return false;
 7270        }
 271
 272        /// <summary>
 273        ///     Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible.
 274        /// </summary>
 275        /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i
 276        /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param>
 277        /// <param name="name">The property's name to read.</param>
 278        /// <param name="returnValue">The returned value from the property, the default value if the property was unable
 279        /// <param name="flags">The property's access flags.</param>
 280        /// <typeparam name="T">The type of data being read from the property.</typeparam>
 281        /// <returns>true/false if the process was successful.</returns>
 282        public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin
 5283        {
 5284            if (type == null)
 1285            {
 1286                returnValue = default;
 1287                return false;
 288            }
 4289            PropertyInfo propertyInfo = type.GetProperty(name, flags);
 4290            if (propertyInfo == null)
 1291            {
 1292                returnValue = default;
 1293                return false;
 294            }
 295
 3296            returnValue = (T)propertyInfo.GetValue(targetObject);
 3297            return true;
 5298        }
 299    }
 300}

Coverage by test methods















































































































































































































































































































































































































































































































































































































































































































Methods/Properties

Reflection()
GetDefault(System.Type)
GetType(System.String)
GetTypeQualifiedName(System.Type)
InvokeStaticMethod(System.String, System.String, System.Object[], System.Reflection.BindingFlags)
InvokeMethod(System.Object, System.String, System.Object[], System.Reflection.BindingFlags)
SetFieldOrPropertyValue(System.Object, System.String, System.Object, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
SetFieldValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
SetPropertyValue(System.Object, System.Type, System.String, System.Object, System.Reflection.BindingFlags)
TryGetFieldValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)
TryGetFieldOrPropertyValue(System.Object, System.String, System.Object&, System.Reflection.BindingFlags, System.Reflection.BindingFlags)
TryGetPropertyValue[T](System.Object, System.Type, System.String, , System.Reflection.BindingFlags)